<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | 王勇程 <laughskydragon@qq.com>                         |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\model;

/**
 * 商品导入日志模型
 */
class ProductImportLog extends ModelBase
{
    
    // 设置当前模型对应的完整数据表名称
    protected $table = 'ob_product_import_log';
    
    // 设置主键
    protected $pk = 'id';
    
    /**
     * 获取导入状态文本
     */
    public function getImportStatusTextAttr($value, $data)
    {
        $status = [
            'processing' => '处理中',
            'completed' => '已完成',
            'failed' => '失败'
        ];
        
        return $status[$data['import_status']] ?? '未知';
    }
    
    /**
     * 获取文件大小文本
     */
    public function getFileSizeTextAttr($value, $data)
    {
        $size = $data['file_size'];
        
        if ($size < 1024) {
            return $size . ' B';
        } elseif ($size < 1024 * 1024) {
            return round($size / 1024, 2) . ' KB';
        } else {
            return round($size / (1024 * 1024), 2) . ' MB';
        }
    }
    
    /**
     * 获取创建时间文本
     */
    public function getCreateTimeTextAttr($value, $data)
    {
        return date('Y-m-d H:i:s', $data['create_time']);
    }
    
    /**
     * 获取开始时间文本
     */
    public function getStartTimeTextAttr($value, $data)
    {
        return $data['start_time'] ? date('Y-m-d H:i:s', $data['start_time']) : '';
    }
    
    /**
     * 获取结束时间文本
     */
    public function getEndTimeTextAttr($value, $data)
    {
        return $data['end_time'] ? date('Y-m-d H:i:s', $data['end_time']) : '';
    }
    
    /**
     * 获取耗时文本
     */
    public function getDurationTextAttr($value, $data)
    {
        if (empty($data['duration'])) {
            return '';
        }
        
        $duration = $data['duration'];
        
        if ($duration < 60) {
            return $duration . ' 秒';
        } elseif ($duration < 3600) {
            return floor($duration / 60) . ' 分 ' . ($duration % 60) . ' 秒';
        } else {
            $hours = floor($duration / 3600);
            $minutes = floor(($duration % 3600) / 60);
            $seconds = $duration % 60;
            return $hours . ' 时 ' . $minutes . ' 分 ' . $seconds . ' 秒';
        }
    }
    
    /**
     * 获取成功率文本
     */
    public function getSuccessRateTextAttr($value, $data)
    {
        if (empty($data['total_rows'])) {
            return '0%';
        }
        
        $rate = ($data['success_rows'] / $data['total_rows']) * 100;
        return round($rate, 2) . '%';
    }
}